
import java.awt.*;
import java.awt.event.*;

import javax.swing.*;
import javax.swing.event.*; 
import java.io.*;
import java.util.*;


public class PolicyFrame implements Serializable, MouseListener {

	/**
	 * 
	 */
	private static final long serialVersionUID = 346614282248890005L;
	PolicyPropertyFrame property;
	NutritionFrame nutrition;
	XmlFrame xmlPolicy;
	WarningFrame warningFrame;
	ValidateFrame validator;
//	JPanel panel;
	JTabbedPane tab;
	/*many things to record*/
	String projectName;
	String projectPath;
	boolean changed;
	
	public PolicyFrame(){
		property=new PolicyPropertyFrame();
		property.config();
		nutrition=new NutritionFrame();
		nutrition.config();
		xmlPolicy=new XmlFrame(this);
		xmlPolicy.config();
		warningFrame=new WarningFrame(this);
		warningFrame.config();
		validator=new ValidateFrame(this);
		projectName="";
		projectPath="";
		changed=false;
	}
	
	public void config(){
		tab=new JTabbedPane();
		tab.setTabPlacement(JTabbedPane.LEFT);
		tab.addMouseListener(this);
		tab.add("policy property",property);
		tab.add("data group",nutrition);
		tab.add("xml policy",xmlPolicy);
		tab.add("warning",warningFrame);
		tab.add("validate",validator);
//		panel=new JPanel();
//		panel.add(tab);
	}

	public String getProjectName() {
		return projectName;
	}

	public void setProjectName(String projectName) {
		this.projectName = projectName;
	}

	public String getProjectPath() {
		return projectPath;
	}

	public void setProjectPath(String projectPath) {
		this.projectPath = projectPath;
	}
	
	public void copyData(PolicyFrame p){
		nutrition.tables=new ArrayList<NutritionTableView>();
		nutrition.tab.removeAll();
		NutritionFrame nf=p.nutrition;
		for(int i=0;i<nf.tables.size();i++){
			NutritionTableView t=nf.tables.get(i);
			String name=t.getName();
			int retention=t.getRetention();
			String description=t.getDescription();
			boolean identifiable=t.isIdentifiable();
			NutritionTableView ntv=new NutritionTableView(name, retention, description,identifiable);
			ntv.config();
			ntv.data=new Object[11][9];
			ntv.data=t.getData();
			ntv.detail=new Detail[11][9];
			ntv.detail=t.getDetail();
			nutrition.tables.add(ntv);
			nutrition.tab.add(name,ntv);
		}
		
		
		projectName=p.projectName;
		projectPath=p.projectPath;
		
		property.pp.organizationName=p.property.pp.organizationName;
		property.pp.emailAddress = p.property.pp.emailAddress;
		property.pp.webHomePage=p.property.pp.webHomePage;
		property.pp.phoneNumber=p.property.pp.phoneNumber;
		
		property.pp.addressName=p.property.pp.addressName;
		property.pp.address=p.property.pp.address;
		property.pp.city=p.property.pp.city;
		property.pp.state=p.property.pp.state;
		property.pp.postalCode=p.property.pp.postalCode;
		property.pp.country=p.property.pp.country;
		//web site
		property.pp.policyName=p.property.pp.policyName;
		property.pp.optUrl=p.property.pp.optUrl;
		property.pp.policyLanguage=p.property.pp.policyLanguage;
		property.pp.readablePolicyUrl=p.property.pp.readablePolicyUrl;
		//access
		property.pp.access=p.property.pp.access;
		property.pp.assurance=new Vector<Assurance>(p.property.pp.assurance);
		
		//expiry
		property.pp.calMode=p.property.pp.calMode; /*true - use lifetime; false - use date*/
		property.pp.lifeTime=p.property.pp.lifeTime;
		property.pp.unit=p.property.pp.unit;
		property.pp.expireDay=p.property.pp.expireDay;
		property.pp.expireMonth=p.property.pp.expireMonth;
		property.pp.expireYear=p.property.pp.expireYear;
		property.pp.forTest=p.property.pp.forTest;
		
//		flushUI();
	}
	
	//push data from ui to variables
	public void flushData(){
		PolicyProperty pp=property.pp;	
		
		//info about organization
		pp.organizationName=property.orgName.getText();
		pp.emailAddress=property.email.getText();
		pp.webHomePage=property.webpage.getText();
		pp.phoneNumber=property.telephone.getText();
		//mailing address
		pp.addressName=property.mailingName.getText();
		pp.address=property.streetAddress.getText();
		pp.city=property.city.getText();
		pp.state=property.state.getText();
		pp.postalCode=property.postal.getText();
		pp.country=property.country.getText();
		//web site
		pp.policyName=property.policyName.getText();
		pp.optUrl=property.optUrl.getText();
		pp.policyLanguage=(String)property.language.getSelectedItem();
		pp.readablePolicyUrl=property.readablePolicy.getText();
		//access		
		if(property.noneCollect.isSelected())
		{
			pp.access=PolicyProperty.ACCESS_NO_DATA_COLLECT;
		}		
		if(property.all.isSelected())
		{
			pp.access=PolicyProperty.ACCESS_ALL;
		}		
		if(property.conAndOth.isSelected())
		{
			pp.access=PolicyProperty.ACCESS_CONTACT_AND_OTHER;
		}		
		if(property.contact.isSelected())
		{
			pp.access=PolicyProperty.ACCESS_CONTACT;
		}		
		if(property.other.isSelected())
		{
			pp.access=PolicyProperty.ACCESS_OTHER;
		}		
		if(property.none.isSelected())
		{
			pp.access=PolicyProperty.ACCESS_NONE;
		}
		
		if(property.byLifetime.isSelected()){
			pp.calMode=true;
		}else{
			if(property.byDate.isSelected())
				pp.calMode=false;
		}
		
		try{
			pp.lifeTime=Integer.valueOf(property.lifetime.getText()).intValue();
		}catch(Exception e){
			pp.lifeTime=1;
		}
		
		pp.unit=(String)property.units.getSelectedItem();
		
		pp.expireDay=(String)property.exD.getSelectedItem();
		pp.expireMonth=(String)property.exM.getSelectedItem();
		pp.expireYear=(String)property.exY.getSelectedItem();
		pp.forTest=property.testOnly.isSelected();
	}
	
	public void flushUI(){
		PolicyProperty pp=property.pp;
		
		NutritionFrame nf=nutrition;
		for(Iterator<NutritionTableView> it=nf.tables.iterator();it.hasNext();){
			it.next().table.repaint();
		}
		
		property.list.setListData(pp.assurance);
		property.list.repaint();
		if(pp.assurance.size()>0){
			property.remove.setEnabled(true);
			property.modify.setEnabled(true);
		}
		//info about organization
		property.orgName.setText(pp.organizationName);
		property.email.setText(pp.emailAddress);
		property.webpage.setText(pp.webHomePage);
		property.telephone.setText(pp.phoneNumber);
		//mailing address
		property.mailingName.setText(pp.addressName);
		property.streetAddress.setText(pp.address);
		property.city.setText(pp.city);
		property.state.setText(pp.state);
		property.postal.setText(pp.postalCode);
		property.country.setText(pp.country);
		//web site
		property.policyName.setText(pp.policyName);
		property.optUrl.setText(pp.optUrl);
		property.language.setSelectedItem(pp.policyLanguage);
		property.readablePolicy.setText(pp.readablePolicyUrl);
		//access		
		if(pp.access==PolicyProperty.ACCESS_NO_DATA_COLLECT)
		{
			property.noneCollect.setSelected(true);
		}		
		if(pp.access==PolicyProperty.ACCESS_ALL)
		{
			property.all.setSelected(true);
		}		
		if(pp.access==PolicyProperty.ACCESS_CONTACT_AND_OTHER)
		{
			property.conAndOth.setSelected(true);
		}		
		if(pp.access==PolicyProperty.ACCESS_CONTACT)
		{
			property.contact.setSelected(true);
		}		
		if(pp.access==PolicyProperty.ACCESS_OTHER)
		{
			property.other.setSelected(true);
		}		
		if(pp.access==PolicyProperty.ACCESS_NONE)
		{
			property.none.setSelected(true);
		}
		
		if(pp.calMode==true){			
			property.byLifetime.setSelected(true);
			property.lifetime.setEnabled(true);
			property.units.setEnabled(true);
			property.exD.setEnabled(false);
			property.exM.setEnabled(false);
			property.exY.setEnabled(false);
		}else{
			property.byDate.setSelected(true);
			property.lifetime.setEnabled(false);
			property.units.setEnabled(false);
			property.exD.setEnabled(true);
			property.exM.setEnabled(true);
			property.exY.setEnabled(true);
		}
		
		property.lifetime.setText(pp.lifeTime+"");
		
		property.units.setSelectedItem(pp.unit);
		
		property.exD.setSelectedItem(pp.expireDay);
		property.exM.setSelectedItem(pp.expireMonth);
		property.exY.setSelectedItem(pp.expireYear);
		property.testOnly.setSelected(pp.forTest);
	}

	/***************mouse listener****************/
	@Override
	public void mouseClicked(MouseEvent e) {
		if(e.getSource().equals(tab)){
			if(tab.getSelectedIndex()!=1){
				JPanel d=MainFrame.getInstance().details.dataType;
				JPanel p=MainFrame.getInstance().details.purpose;
				JPanel r=MainFrame.getInstance().details.recipient;
					((CardLayout)d.getLayout()).show(d,"space");
					((CardLayout)p.getLayout()).show(p,"space");
					((CardLayout)r.getLayout()).show(r,"space");
			}
			if(tab.getSelectedIndex()==2){
				xmlPolicy.refresh();
			}
			if(tab.getSelectedIndex()==3){
				warningFrame.refresh();
			}
			if(tab.getSelectedIndex()==4){
				validator.refresh();
			}
		}
	}

	@Override
	public void mouseEntered(MouseEvent e) {}

	@Override
	public void mouseExited(MouseEvent e) {}

	@Override
	public void mousePressed(MouseEvent e) {}

	@Override
	public void mouseReleased(MouseEvent e) {}
}
